package com.contory.ssn_lgnd.app.comp;

import com.contory.ssn_lgnd.app.Assets;
import com.contory.ssn_lgnd.framework.Input.TouchEvent;
import com.contory.ssn_lgnd.framework.gl.SpriteBatcher;
import com.contory.ssn_lgnd.framework.gl.TextureRegion;
import com.contory.ssn_lgnd.framework.math.OverlapTester;

public class ToggleObject extends ButtonObject{
	public boolean IsDisable = true;
	TextureRegion tr_disable = null;
	
	@Override
	public void update(float deltaTime) {
		if(touchEvent != null){
			switch(state_ptr){
				case idle:
					if(touchEvent.type == TouchEvent.TOUCH_DOWN){
						if(OverlapTester.pointInRectangle(bound, touchPoint)){
							r = g = b = a = 0.5f;
							state_ptr = clicked;
							if(sound != null)
								Assets.playSound(sound);
						}
					}
				break;
				case clicked:
					if(touchEvent.type == TouchEvent.TOUCH_UP){
						if(OverlapTester.pointInRectangle(bound, touchPoint)){
							state_ptr = excute;
						}
					}
					else if(touchEvent.type == TouchEvent.TOUCH_DRAGGED){
						if(!OverlapTester.pointInRectangle(bound, touchPoint)){
							r = g = b = a = 1.0f;
							state_ptr = idle;
						}
					}
				break;
				case excute:
					if(IsDisable)
						IsDisable = false;
					else
						IsDisable = true;
					r = g = b = a = 1.0f;
					state_ptr = idle;
					excuteBool = true;
					if(todo != null)
						todo.todo();
				break;
			}
		}
	}

	@Override
	public void draw(SpriteBatcher batcher) {
		if(tr != null)
			batcher.drawSprite(x, y, w, h, r, g, b, a, tr);
		if(name != "")
			Assets.font.drawText(batcher, name, x, y, nameSize, nameSize, r, g, b, a);
		if(tr_icon != null)
			batcher.drawSprite(x, y, iconSize, iconSize, r, g, b, a, tr_icon);
		if(tr_disable != null && IsDisable)
			batcher.drawSprite(x, y, iconSize, iconSize, r, g, b, a, tr_disable);
	}
	
	public void setXY_WH_TR(float x, float y, float w, float h, TextureRegion tr, TextureRegion tr_icon, TextureRegion tr_disable, byte enable){
		super.setXY_WH_TR(x, y, w, h, tr);
		if(enable > 0)
			this.IsDisable = false;
		else
			this.IsDisable = true;
		this.tr_icon = tr_icon;
		this.tr_disable = tr_disable;
	}
}
